package com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.entity.SmsTeachAdministrativeStaffCheckInApplicationProcess;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.entity.dto.SmsTeachAdministrativeStaffCheckInApplicationProcessDTO;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.entity.vo.SmsTeachAdministrativeStaffCheckInApplicationProcessVO;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.mapper.SmsTeachAdministrativeStaffCheckInApplicationProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.service.SmsTeachAdministrativeStaffCheckInApplicationProcessService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.constant.CheckType.PROCESS_PEOPLE_CHECK_TYPE;
import static com.xyht.sca_s.student_manage_system.modules.teachAndStaffCheckInApplication.constant.CheckType.PROCESS_ROLE_CHECK_TYPE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-05
 */
@Service
public class SmsTeachAdministrativeStaffCheckInApplicationProcessServiceImpl extends ServiceImpl<SmsTeachAdministrativeStaffCheckInApplicationProcessMapper, SmsTeachAdministrativeStaffCheckInApplicationProcess> implements SmsTeachAdministrativeStaffCheckInApplicationProcessService {
    @Autowired
    SmsTeachAdministrativeStaffCheckInApplicationProcessMapper smsTeachAdministrativeStaffCheckInApplicationProcessMapper;
    @Autowired
    SmsUserMapper smsUserMapper;
    @Autowired
    SmsRoleMapper smsRoleMapper;
    @Autowired
    CacheUtil cacheUtil;
    @Override
    public ResponseResult smsTeachAdministrativeStaffCheckInApplicationProcessAdd(SmsTeachAdministrativeStaffCheckInApplicationProcessVO
                                                                                  smsTeachAdministrativeStaffCheckInApplicationProcessVO) {
        if(isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //判断设置的checkId是否存在
        if(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType()==PROCESS_PEOPLE_CHECK_TYPE){//设置的审批人为个人时
            QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SmsUser::getId,smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId());
            SmsUser smsUser = smsUserMapper.selectOne(queryWrapper);
            if(isNullOrEmpty(smsUser)){
                return CommonResult.failed(CommonCodeEnum.PROCESS_PERSON_NOT_EXIST);
            }
            SmsTeachAdministrativeStaffCheckInApplicationProcess smsTeachAdministrativeStaffCheckInApplicationProcess =
                    new SmsTeachAdministrativeStaffCheckInApplicationProcess();
            Integer sortCount = smsTeachAdministrativeStaffCheckInApplicationProcessMapper.selectCount(new QueryWrapper<>());
            Integer newSortCount = sortCount+1;
            smsTeachAdministrativeStaffCheckInApplicationProcess.setSort(newSortCount);
            smsTeachAdministrativeStaffCheckInApplicationProcess.setUserName(smsUser.getRealName());
            smsTeachAdministrativeStaffCheckInApplicationProcess.setCheckId(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId());
            smsTeachAdministrativeStaffCheckInApplicationProcess.setCheckType(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType());
            smsTeachAdministrativeStaffCheckInApplicationProcessMapper.insert(smsTeachAdministrativeStaffCheckInApplicationProcess);
        }
        if(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType()==PROCESS_ROLE_CHECK_TYPE){//设置的审批人为角色时
            QueryWrapper<SmsRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SmsRole::getId,smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId());
            SmsRole smsRole = smsRoleMapper.selectOne(queryWrapper);
            if(isNullOrEmpty(smsRole)){
                return CommonResult.failed(CommonCodeEnum.PROCESS_ROLE_NOT_EXIST);
            }
            SmsTeachAdministrativeStaffCheckInApplicationProcess smsTeachAdministrativeStaffCheckInApplicationProcess =
                    new SmsTeachAdministrativeStaffCheckInApplicationProcess();
            Integer sortCount = smsTeachAdministrativeStaffCheckInApplicationProcessMapper.selectCount(new QueryWrapper<>());
            Integer newSortCount = sortCount+1;
            smsTeachAdministrativeStaffCheckInApplicationProcess.setRoleName(smsRole.getRoleName());
            smsTeachAdministrativeStaffCheckInApplicationProcess.setSort(newSortCount);
            smsTeachAdministrativeStaffCheckInApplicationProcess.setCheckId(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId());
            smsTeachAdministrativeStaffCheckInApplicationProcess.setCheckType(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType());
            smsTeachAdministrativeStaffCheckInApplicationProcessMapper.insert(smsTeachAdministrativeStaffCheckInApplicationProcess);

        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteSmsTeachAdministrativeStaffCheckInApplicationProcess(SmsTeachAdministrativeStaffCheckInApplicationProcessVO
                                                                                     smsTeachAdministrativeStaffCheckInApplicationProcessVO) {
        if(isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsTeachAdministrativeStaffCheckInApplicationProcess query = smsTeachAdministrativeStaffCheckInApplicationProcessMapper
                .selectById(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId());
        if(isNullOrEmpty(query)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        Integer sortCount = smsTeachAdministrativeStaffCheckInApplicationProcessMapper.selectCount(new QueryWrapper<>());
        if(query.getSort().equals(sortCount)){
            smsTeachAdministrativeStaffCheckInApplicationProcessMapper
                    .deleteById(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId());
        }
        else{
            return CommonResult.failed(CommonCodeEnum.TEACH_STAFF_NOT_LAST_ONE);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateSmsTeachAdministrativeStaffCheckInApplicationProcess(SmsTeachAdministrativeStaffCheckInApplicationProcessVO
                                                                                     smsTeachAdministrativeStaffCheckInApplicationProcessVO) {
        if(isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if(isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getProcessIsApplicantEdit()
                &&smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType()==PROCESS_ROLE_CHECK_TYPE){
            if(isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId())){
                return CommonResult.failed(CommonCodeEnum.TEACH_STAFF_PROCESS_NOT_TYPE);
            }

        }
        SmsTeachAdministrativeStaffCheckInApplicationProcess smsTeachAdministrativeStaffCheckInApplicationProcess =
                smsTeachAdministrativeStaffCheckInApplicationProcessMapper.selectById(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId());
        if(smsTeachAdministrativeStaffCheckInApplicationProcess==null||isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcess.getId())){
            return CommonResult.failed(CommonCodeEnum.TEACH_STAFF_PROCESS_NOT_EXIST);
        }
        if(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType()!=null&&
                !isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId())){
                if(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType()==PROCESS_PEOPLE_CHECK_TYPE
                &&!smsTeachAdministrativeStaffCheckInApplicationProcessVO.getProcessIsApplicantEdit()){
                    SmsUser smsUser = cacheUtil.getUserInfo(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId());
                    if(smsUser==null||isNullOrEmpty(smsUser.getId())){
                        return CommonResult.failed(CommonCodeEnum.TEACH_STAFF_PROCESS_USER_NOT_EXIST);
                    }
                }else if(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType()==PROCESS_ROLE_CHECK_TYPE){
                    SmsRole smsRole = smsRoleMapper.selectById(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId());
                    if(smsRole==null||isNullOrEmpty(smsRole.getId())){
                        return CommonResult.failed(CommonCodeEnum.TEACH_STAFF_PROCESS_ROLE_NOT_EXIST);
                    }
                }
        }
        SmsTeachAdministrativeStaffCheckInApplicationProcess updateProcess = new SmsTeachAdministrativeStaffCheckInApplicationProcess();
        updateProcess.setId(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId());
        if(!isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType())){
            updateProcess.setCheckId(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId());
            updateProcess.setCheckType(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType());
        }
        if(!isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getIsSign())){
            updateProcess.setIsSign(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getIsSign());
        }
        if(!isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getIsStamp())){
            updateProcess.setIsStamp(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getIsStamp());
        }
        if(!isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getIsUse())){
            updateProcess.setIsUse(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getIsUse());
        }
        if(!isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getProcessIsApplicantEdit())){
            updateProcess.setProcessIsApplicantEdit(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getProcessIsApplicantEdit());
        }

        smsTeachAdministrativeStaffCheckInApplicationProcessMapper.updateById(updateProcess);
        return CommonResult.success();



//        if(isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId())||
//           isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId())||
//           isNullOrEmpty(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType())){
//            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
//        }
//        SmsTeachAdministrativeStaffCheckInApplicationProcess query = smsTeachAdministrativeStaffCheckInApplicationProcessMapper
//                .selectById(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getId());
//        if(query==null){
//            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
//        }
//        //如果修改的是个人
//        if(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType()==PROCESS_PEOPLE_CHECK_TYPE) {
//            QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
//            queryWrapper.lambda().eq(SmsUser::getId, smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId());
//            SmsUser smsUser = smsUserMapper.selectOne(queryWrapper);
//            if (isNullOrEmpty(smsUser)) {
//                return CommonResult.failed(CommonCodeEnum.PROCESS_PERSON_NOT_EXIST);
//            }
//        }
//        //如果修改的是角色
//        if(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckType()==PROCESS_ROLE_CHECK_TYPE){
//            QueryWrapper<SmsRole> queryWrapper = new QueryWrapper<>();
//            queryWrapper.lambda().eq(SmsRole::getId,smsTeachAdministrativeStaffCheckInApplicationProcessVO.getCheckId());
//            SmsRole smsRole = smsRoleMapper.selectOne(queryWrapper);
//            if(isNullOrEmpty(smsRole)){
//                return CommonResult.failed(CommonCodeEnum.PROCESS_ROLE_NOT_EXIST);
//            }
//        }
//        SmsTeachAdministrativeStaffCheckInApplicationProcess smsTeachAdministrativeStaffCheckInApplicationProcess
//                = new SmsTeachAdministrativeStaffCheckInApplicationProcess();
//        BeanUtils.copyProperties(smsTeachAdministrativeStaffCheckInApplicationProcessVO, smsTeachAdministrativeStaffCheckInApplicationProcess);
//        smsTeachAdministrativeStaffCheckInApplicationProcess.setSort(smsTeachAdministrativeStaffCheckInApplicationProcessVO.getSort());
//        smsTeachAdministrativeStaffCheckInApplicationProcessMapper.updateById(smsTeachAdministrativeStaffCheckInApplicationProcess);
//        return CommonResult.success();
    }

    @Override
    public ResponseResult selectSmsTeachAdministrativeStaffCheckInApplicationProcess() {
        List<SmsTeachAdministrativeStaffCheckInApplicationProcessDTO> processDTOList =
                smsTeachAdministrativeStaffCheckInApplicationProcessMapper.selectList(new QueryWrapper<SmsTeachAdministrativeStaffCheckInApplicationProcess>()
                        .lambda()
                        .orderByAsc(SmsTeachAdministrativeStaffCheckInApplicationProcess::getSort))
                        .stream()
                        .map(process -> {
                            SmsTeachAdministrativeStaffCheckInApplicationProcessDTO processDTO =
                                    new SmsTeachAdministrativeStaffCheckInApplicationProcessDTO();
                            BeanUtils.copyProperties(process, processDTO);

                            if (process.getCheckType() != null && !isNullOrEmpty(process.getCheckId())) {
                                if (process.getCheckType() == PROCESS_PEOPLE_CHECK_TYPE) {

                                    SmsUser smsUser = cacheUtil.getUserInfo(process.getCheckId());
                                    if (smsUser != null && !isNullOrEmpty(smsUser.getId())) {
                                        processDTO.setProcessObjectName(smsUser.getRealName());
                                        processDTO.setRealName(smsUser.getRealName());
                                        processDTO.setHeadPic(smsUser.getUserPic());
                                    }
                                } else {
                                    SmsRole smsRole = smsRoleMapper.selectById(process.getCheckId());
                                    if (smsRole != null && !isNullOrEmpty(smsRole.getId())) {
                                        processDTO.setProcessObjectName(smsRole.getRoleName());
                                    }
                                }
                            }
                            return processDTO;
                        }).collect(Collectors.toList());
        return CommonResult.success(processDTOList);
    }

//        QueryWrapper<SmsTeachAdministrativeStaffCheckInApplicationProcess> queryWrapper = new QueryWrapper<>();
//        if(isNullOrEmpty(queryWrapper)){
//            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
//        }
//        queryWrapper.orderByAsc("sort");
//
//        List<SmsTeachAdministrativeStaffCheckInApplicationProcess> list = smsTeachAdministrativeStaffCheckInApplicationProcessMapper.selectList(queryWrapper);
//        if (list.size() == 0) {
//            return CommonResult.success(list);
//        }
//
//        return CommonResult.success(list);


    @Override
    public ResponseResult selectSmsTeachAdministrativeStaffCheckInApplicationProcessByAllowPeople(SmsTeachAdministrativeStaffCheckInApplicationProcessVO
                                                                                                  smsTeachAdministrativeStaffCheckInApplicationProcessVO) {
        return null;
    }

    @Override
    public ResponseResult selectSmsTeachAdministrativeStaffCheckInApplicationProcessById(String id) {
        if(isNullOrEmpty(id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsTeachAdministrativeStaffCheckInApplicationProcess query = smsTeachAdministrativeStaffCheckInApplicationProcessMapper
                .selectById(id);
        if(query==null||query.getId()==null){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        SmsTeachAdministrativeStaffCheckInApplicationProcess smsTeachAdministrativeStaffCheckInApplicationProcess
                = new SmsTeachAdministrativeStaffCheckInApplicationProcess();
        smsTeachAdministrativeStaffCheckInApplicationProcess.setId(id);

        SmsTeachAdministrativeStaffCheckInApplicationProcessDTO smsTeachAdministrativeStaffCheckInApplicationProcessDTO
                = new SmsTeachAdministrativeStaffCheckInApplicationProcessDTO();
        BeanUtils.copyProperties(query,smsTeachAdministrativeStaffCheckInApplicationProcessDTO);
        return CommonResult.success(smsTeachAdministrativeStaffCheckInApplicationProcessDTO);
    }
}
